package com.css.fxfzaqbz.zcpt.sys.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.util.CreateGson;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.service.SysMenuService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Dbj
 * @date 2022年08月10日  11:17
 */
@RestController
@RequestMapping("/fxfz/smenu")
@Slf4j
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 根据用户id获取用户所属的主部门信息
     */
    @GetMapping("/getMenuByUserId")
    public RestResponse getMenuByUserId() {
        String userId = PlatformSessionContext.getUserID();
        RestResponse restResponse = null;
        if(org.apache.commons.lang3.StringUtils.isBlank(userId)){
            restResponse = RestResponse.fail("缺失用户id!");
            return restResponse;
        }
        try{
            String jsonStr = sysMenuService.getMenuListByUserId(userId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询菜单失败!",e);
            restResponse = RestResponse.fail("查询菜单失败!");
        }
        return restResponse;
    }
}
